﻿Partial Public Class SOPExplorer

    Private aDataRowAttribute As DataRow
    Private aDataRowSOPRev As DataRow
    Private aMetaGraphForm As MetaGraphForm

 
    Public Sub TreeActionOptionListLoad()
        Me.MetaControlTreeListActionOptionList.Add(New ActionOption("SOPOption", "ACT_00179", "Create new revision", AddressOf CreateNewSOPvision, True, True, 1))
    End Sub


    Public Overridable Sub CreateNewSOPvision(ByVal sender As Object, ByVal e As EventArgs)
        'Dim node As TreeListNode = TreeList1.FocusedNode
        Dim item As DXMenuItem = TryCast(sender, DXMenuItem)
        '  Dim node As TreeListNode = TryCast(item.Tag, TreeListNode)

        Try
            SystemBoxSDK.PL.CoreSDK.SOPRevision_PL.CreateNewSOPRevision(DirectCast(NodeDataRow("KeyID"), Guid))
            ReloadTree()
        Catch sqlex As System.Data.SqlClient.SqlException
            MessageWindow.ShowMessage(sqlex)
        End Try
    End Sub


    Private Sub EditDocument(ByVal sender As Object, ByVal e As EventArgs)
        Dim aDataRowSOPRev As DataRow

        Me.Cursor = Cursors.WaitCursor
        aDataRowAttribute = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Try
            If aDataRowAttribute IsNot Nothing Then
                aDataRowSOPRev = CoreSDK.SOPRevision_PL.GetSOPRevisionByID(CType(aDataRowAttribute("SOPRevisionId_FK"), Guid))
                If aDataRowAttribute IsNot Nothing Then
                    aDocGen = New DocGen() With {.MdiParent = Me.ParentForm, .ShowIcon = True}
                    aDocGen.IdOfMergedDoc = CType(aDataRowAttribute("SOPAttributeId"), Guid)
                    aDocGen.DocumentTemplateID = New System.Guid("0BD794D0-5FD7-496C-B628-F52DA318A077")
                    If Not IsDBNull(aDataRowAttribute("DocumentData")) Then
                        aDocGen.DocumentData = aDataRowAttribute("DocumentData").ToString
                    End If
                    '
                    If CBool(aDataRowSOPRev("IsActivated")) = True Then
                        aDocGen.ActivatedDocument = True
                    End If
                    '
                    AddHandler aDocGen.SaveMergedDoc, AddressOf SavedMergeSOP
                    aDocGen.Show()
                End If
            End If
        Catch ex As Exception
            MessageWindow.ShowMessage(ex.Message, MsgBoxStyle.Information)
        End Try
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub EditGraphique(ByVal sender As Object, ByVal e As EventArgs)
        Me.Cursor = Cursors.WaitCursor
        aDataRowAttribute = CType(CType(sender, DXMenuItem).Tag, DataRow)

        Dim binarydata() As Byte
        Dim binaryStream As IO.MemoryStream = Nothing
        Try
            If aDataRowAttribute IsNot Nothing Then
                aDataRowSOPRev = CoreSDK.SOPRevision_PL.GetSOPRevisionByID(CType(aDataRowAttribute("SOPRevisionId_FK"), Guid))
                If aDataRowAttribute IsNot Nothing Then
                    aMetaGraphForm = New MetaGraphForm()
                    aMetaGraphForm.MetaControlCd = Me.PanelName
                    aMetaGraphForm.Id = aDataRowAttribute("SOPAttributeId")
                    '
                    If Not IsDBNull(aDataRowAttribute("DocumentData")) Then
                        binaryStream = New IO.MemoryStream()
                        binarydata = Convert.FromBase64String(aDataRowAttribute("DocumentData").ToString)
                        binaryStream.Write(binarydata, 0, binarydata.Length)
                        binaryStream.Position = 0
                        aMetaGraphForm.DocumentStream = binaryStream
                    End If
                    '
                    If CBool(aDataRowSOPRev("IsActivated")) = True Then
                        aMetaGraphForm.ActivatedDocument = True
                    End If
                    '
                    'AddHandler aMetaGraphForm.SaveToDataBase, AddressOf SaveToDataBase
                    aMetaGraphForm.ShowDialog()
                    aGridControl.ReloadGrid()
                End If
            End If
        Catch ex As Exception
            MessageWindow.ShowMessage(ex.Message, MsgBoxStyle.Information)
        End Try
        Me.Cursor = Cursors.Default
    End Sub


    Public Sub SavedMergeSOP(ByVal id As Guid?, ByVal text As String)
        Try
            CoreSDK.SOPAttribute_PL.SaveAttributeDocument(id, text, Nothing)
            aGridControl.ReloadGrid()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub


    Private Sub InitNewMasterRow(sender As Object, e As InitNewRowEventArgs)
        Dim view As GridView = CType(sender, GridView)
        Dim row As DataRow = view.GetFocusedDataRow()
        Using SOP As SystemBoxSDK.PL.CoreSDK.SOP_PL = New SystemBoxSDK.PL.CoreSDK.SOP_PL()
            SOP.NewRecord(row)
        End Using
    End Sub


End Class
